package util;

import java.util.Iterator;

public class BinaryCombinationsIterator implements Iterator<int[]> {
	 
    private int current;
    private final int bits;
    private final int max;
    
    public BinaryCombinationsIterator(int n) {
        current = 0;
        bits = n;
        max = (int)Math.pow(2, bits);
    }
 
    public boolean hasNext() { 
        return current < max; 
    }
 
    public int[] next() {
        if(!hasNext())
            throw new RuntimeException("No more combinations left!");
        int[] combination = new int[bits];
        for(int i = bits-1, j = 1; i >= 0; i--, j<<=1)
            combination[i] = (int)((current&j) > 0 ? 1 : -1);
        current++;
        return combination;
    }
 
    public void remove() {
        throw new UnsupportedOperationException("Geht nicht!");
    }
}